package com.babyshop.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.babyshop.domain.Comment;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class CommentDaoImpl implements CommentDao {

	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	@Override
	public void saveOfUpdate(Comment comment) {
		try {
			session.saveOrUpdate(comment);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public void delete(int id) {
		try {
			Comment comment = (Comment) session.get(Comment.class, id);
			session.delete(comment);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public Comment get(int id) {
		Comment comment = null;
		try {
			comment = (Comment) session.get(Comment.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return comment;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Comment> getAll() {
		List<Comment> listComments = null;
		try {
			listComments = session.createQuery("from Comment").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listComments;
	}

}
